package com.gmrz.uap.service.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.gmrz.uap.TenantMapper;
import com.gmrz.uap.aop.SystemInterceptor;
import com.gmrz.uap.config.Constants;
import com.gmrz.uap.entity.Tenant;
import com.gmrz.uap.service.TenantService;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Wang Haifeng
 */
public class TenantServiceImpl implements TenantService {
    private static final Logger log = LoggerFactory.getLogger(TenantServiceImpl.class);

    private final TenantMapper tenantMapper;

    @Inject
    public TenantServiceImpl(TenantMapper tenantMapper) {
        this.tenantMapper = tenantMapper;
    }

    @Override
    @SystemInterceptor
    public String getAllTenant(Tenant tenant, String param) {
        log.info("getAllTenant:{}", tenant);
        log.info("param:{}", param);

        try (Entry entry = SphU.entry(Constants.FLOW_RULE_REG_RECEIVE)) {
            return JSONUtil.toJsonStr(tenantMapper.selectAll().get(0));
        } catch (Exception e) {
            log.error("getAllTenant error", e);
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("code", 500);
            jsonObject.set("msg", "getAllTenant error");
            return jsonObject.toString();
        }
    }

    @Override
    public String getTenantName(String tenantId) {
        return tenantId;
    }
}
